# coding: utf-8
from flask import Flask, render_template,request,jsonify
from flask_sqlalchemy import SQLAlchemy


app = Flask(__name__, static_url_path='')  # 静态目录路径，访问可以  /admin/js/
app.debug = True
app.secret_key='9024j;sf;jas;jf;adfsf'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///./db/pms.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.config['SQLALCHEMY_ECHO'] = True

db = SQLAlchemy(app)

from pms_app import emp as blueprint_emp
app.register_blueprint(blueprint_emp, url_prefix='/emp')

from admin import admin as blueprint_admin
app.register_blueprint(blueprint_admin,url_prefix='/admin')

@app.route('/')
def index():
    return render_template('emp-list-ajax.html')

@app.route('/ajax/')
def ajax_temp():
    return render_template('test-ajax.html')


@app.route('/ajax_calc/')
def ajax_calc():
    a=int(request.args.get('a',0))
    b=int(request.args.get('b',0))
    return str(a*b)

@app.route('/jquery-ajax/')
def jquery_ajax():
    return render_template('jquery-ajax.html')

@app.route('/content/')
def ajax_content():
    text='''
    Lorem ipsum dolor sit amet, 
    <div style="background:#f00">consectetur adipisicing elit. Earum enim fuga </div>
    <p>labore molestiae nobis nostrum odit pariatur, perspiciatis tenetur <a href="#">优品课堂</a>
    unde. </p>
    <div class="my-cls">Alias dolorum minima modi nam optio praesentium sit temporibus ut.</div>
    '''
    return text

@app.route('/reg/',methods=['GET','POST'])
def reg_user():
    from pms_app.forms import RegUserForm
    form = RegUserForm(request.form)
    if form.validate and request.method=='POST': # form.validate 新版没有()
    # if form.validate_on_submit:
        return str(form.data)
    return render_template('reg_user.html',form=form)


@app.route('/reg2/',methods=['GET','POST'])
def reg_user2():
    if request.method == 'POST' :  # and request.is_xhr:  # is_xhr请求 新版被移除
        username = request.form.get('username','')
        pwd = request.form.get('pwd','')
        email = request.form.get('email','')
        return jsonify(email)
    return render_template('reg_user2.html')

@app.route('/checkuser/')
# jquery Validate 验证用户名不能重复
def checkuser():
    name = request.args.get("username",'')
    names = ['tom','jerry','john']
    return jsonify(name.lower() not in names)

if __name__ == '__main__':
    print(app.url_map)
    app.run()
